package com.wsc.wisdomchina.activitity.wisdomParty;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseFragmentActivity;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ShowChatViewInterface;
import com.wsc.wisdomchina.fragment.PartyActivityFragment;
import com.wsc.wisdomchina.fragment.PartyAnounceFragment;
import com.wsc.wisdomchina.fragment.PartyChatFragment;
import com.wsc.wisdomchina.fragment.PartyContactFragment;
import com.wsc.wisdomchina.fragment.PartyFragment;
import com.wsc.wisdomchina.fragment.PartyLineTestFragment;
import com.wsc.wisdomchina.fragment.PartyNewsFragment;
import com.wsc.wisdomchina.view.MyToast;
import com.wsc.wisdomchina.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;


public class PartyActivity extends BaseFragmentActivity implements View.OnClickListener,ShowChatViewInterface{
    private ImageView iv_back;
    private MyToast myToast;
    private Context context;
    private String cid;
    private String pId;
    private ImageView iv_user;
    private ViewPager viewPager;
    private PagerSlidingTabStrip tabs;// 新闻栏滑动条
    private List<String> listData;
    private List<Fragment> lastlistfragmentView;
    private MyApplication myApplication;
    private final String PARTY_STUDY="党员学习";
    private final String PARTY_LINETEST="在线答题";
    private final String PARTY_ACTIVITY="党建活动";
    private final String PARTY_CONTACT="通讯录";
    private final String PARTY_ANOUNCE="公告通知";
    private final String PARTY_NEWS="党建资讯";
    private final String PARTY_CHAT="交流园地";
    private ImageView iv_edit;
    private List<String> lastEightModuleFragment;
    private List<Drawable> listSeletedDrawable;
    private List<Drawable> listUnseletedDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.party_layout);
        Intent intent=getIntent();
        cid=intent.getStringExtra("cId");
        pId=intent.getStringExtra("pId");
        myApplication = (MyApplication)getApplication();
        listSeletedDrawable=new ArrayList<>();
        listUnseletedDrawable=new ArrayList<>();

        listSeletedDrawable.add(getResources().getDrawable(R.mipmap.dyxx_selected));
        listSeletedDrawable.add(getResources().getDrawable(R.mipmap.zxdy_seleted));
        listSeletedDrawable.add(getResources().getDrawable(R.mipmap.djhd_seleted));
        listSeletedDrawable.add(getResources().getDrawable(R.mipmap.txl_seleted));
        listSeletedDrawable.add(getResources().getDrawable(R.mipmap.ggtz_seleted));
        listSeletedDrawable.add(getResources().getDrawable(R.mipmap.zixun_seleted));
        listSeletedDrawable.add(getResources().getDrawable(R.mipmap.jlyd_seleted));

        listUnseletedDrawable.add(getResources().getDrawable(R.mipmap.dyxx_unseleted));
        listUnseletedDrawable.add(getResources().getDrawable(R.mipmap.zxdy_unseleted));
        listUnseletedDrawable.add(getResources().getDrawable(R.mipmap.djhd_unseleted));
        listUnseletedDrawable.add(getResources().getDrawable(R.mipmap.txl_unseleted));
        listUnseletedDrawable.add(getResources().getDrawable(R.mipmap.ggtz_unseleted));
        listUnseletedDrawable.add(getResources().getDrawable(R.mipmap.zixun_unseleted));
        listUnseletedDrawable.add(getResources().getDrawable(R.mipmap.jlyd_unseleted));


        initView();
        listData=new ArrayList<>();
        listData.add(PARTY_STUDY);
        listData.add(PARTY_LINETEST);
        listData.add(PARTY_ACTIVITY);
        listData.add(PARTY_CONTACT);
        listData.add(PARTY_ANOUNCE);
        listData.add(PARTY_NEWS);
        listData.add(PARTY_CHAT);
        if(listData!=null && listData.size()>0 )
        {
            showTabsViewPager();
        }
    }

    private void  showTabsViewPager()
    {
        lastlistfragmentView = new ArrayList<>();
        lastEightModuleFragment = new ArrayList<>();
        List<String> nameList=new ArrayList<>();
        for(String data:listData)
        {
            Bundle bundle = new Bundle();
            bundle.putString("cid",cid);
            bundle.putString("pId",pId);
            bundle.putString("name",data);
            if(data.equals(PARTY_STUDY))
            {
                //党员学习
                PartyFragment receptionCenterFragment = PartyFragment.newInstance(bundle);
                lastlistfragmentView.add(receptionCenterFragment);
                lastEightModuleFragment.add(PartyFragment.class.getSimpleName());
                nameList.add(data);
            }else if(data.equals(PARTY_LINETEST))
            {
                //在线答题
                PartyLineTestFragment partyLineTestFragment = PartyLineTestFragment.newInstance(bundle);
                lastlistfragmentView.add(partyLineTestFragment);
                lastEightModuleFragment.add(PartyLineTestFragment.class.getSimpleName());
                nameList.add(data);
            }else if(data.equals(PARTY_ACTIVITY))
            {
                //党建活动
                PartyActivityFragment partyActivityFragment = PartyActivityFragment.newInstance(bundle);
                lastlistfragmentView.add(partyActivityFragment);
                lastEightModuleFragment.add(PartyActivityFragment.class.getSimpleName());
                nameList.add(data);
            }else if(data.equals(PARTY_CONTACT))
            {
                //通讯录
                PartyContactFragment partyContactFragment = PartyContactFragment.newInstance(bundle);
                lastlistfragmentView.add(partyContactFragment);
                lastEightModuleFragment.add(PartyContactFragment.class.getSimpleName());
                nameList.add(data);
            }else if(data.equals(PARTY_ANOUNCE))
            {
                //公告通知
                PartyAnounceFragment partyAnounceFragment = PartyAnounceFragment.newInstance(bundle);
                lastlistfragmentView.add(partyAnounceFragment);
                lastEightModuleFragment.add(PartyAnounceFragment.class.getSimpleName());
                nameList.add(data);
            }else if(data.equals(PARTY_NEWS))
            {
                //党建资讯
                PartyNewsFragment partyNewsFragment = PartyNewsFragment.newInstance(bundle);
                lastlistfragmentView.add(partyNewsFragment);
                lastEightModuleFragment.add(PartyNewsFragment.class.getSimpleName());
                nameList.add(data);
            }else if(data.equals(PARTY_CHAT))
            {
                //交流园地
                PartyChatFragment partyChatFragment = PartyChatFragment.newInstance(bundle);
                lastlistfragmentView.add(partyChatFragment);
                lastEightModuleFragment.add(PartyChatFragment.class.getSimpleName());
                nameList.add(data);
            }
        }
        myApplication.setInformationFragmentSelected(lastEightModuleFragment.get(0));
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), lastlistfragmentView, nameList));// 设置viewpageradapter
        tabs.setViewPager(viewPager, lastlistfragmentView, lastEightModuleFragment, this);
    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> listfragmentView;// viewList 集合
        private List<String> TITLES;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> listfragmentView, List<String> TITLES) {
            super(fm);
            this.listfragmentView = listfragmentView;
            this.TITLES = TITLES;
            // TODO Auto-generated constructor stub
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return TITLES.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return listfragmentView.get(position);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return TITLES.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub

            Fragment ff = (Fragment) super.instantiateItem(container, position);
            ff.setTargetFragment(listfragmentView.get(position), position);

            return ff;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            // TODO Auto-generated method stub
            super.destroyItem(container, position, object);
        }

    }


    private void initView() {
        context = this;
        myToast = new MyToast(context);
        iv_back=(ImageView) findViewById(R.id.iv_back);
        iv_user=(ImageView) findViewById(R.id.iv_user);
        iv_back.setOnClickListener(this);
        iv_user.setOnClickListener(this);

        viewPager = (ViewPager)findViewById(R.id.viewPager);
        tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        tabs.setIndicatorHeight(Tools.dip2px(context, 3));
        tabs.setTextSize(Tools.dip2px(context, 17));
        tabs.setTextColor(getResources().getColor(R.color.d7white));
        tabs.setSelectedTextColor(getResources().getColor(R.color.white));
        tabs.setSeletedHeadImage(true);
        tabs.setListSeletedDrawable(listSeletedDrawable);
        tabs.setListUnseletedDrawable(listUnseletedDrawable);
        iv_edit=(ImageView) findViewById(R.id.iv_edit);
        iv_edit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_user:
                Intent intent=new Intent(context,PartyUserActivity.class);
                startActivityForResult(intent,0);
                break;
            //交流园地编辑页
            case R.id.iv_edit:
                Intent intent1=new Intent(context,PartyChatPublishActivity.class);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void isShowChatView(boolean isShow) {
        if(isShow)
        {
            iv_edit.setVisibility(View.VISIBLE);
        }else {
            iv_edit.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==1)
        {
            finish();
        }
    }

}
